#pragma once
#include <set>
#include <vector>
#include "Poller.h"

namespace xncc {
namespace foundation {
    class Channel;
    class SelectPoller final : public Poller
    {
      public:
        SelectPoller();
        ~SelectPoller() override;
        SelectPoller(const SelectPoller&)             = delete;
        SelectPoller(const SelectPoller&&)            = delete;
        SelectPoller& operator=(const SelectPoller&)  = delete;
        SelectPoller& operator=(const SelectPoller&&) = delete;

        auto ioType() -> types::IO_MULTI_MODE::value override { return types::IO_MULTI_MODE::value::SELECT; }
        ///< 添加一个channel  相当于把一个新的fd 注册到 Select里了， 一个channel 关联一个fd
        void addChannel(const std::shared_ptr<Channel>& chnl) override;
        void removeChannel(const std::shared_ptr<Channel>& chnl) override;
        void updateChannel(const std::shared_ptr<Channel>& chnl) override;
        void loopOnce(int waitUs) override;

      private:
        std::set<int>    readList_;
        std::set<int>    writeList_;
        std::set<int>    exceptList_;  // 只支持TCP带外数据暂时不处理
        std::vector<int> readRedayList_;
        std::vector<int> writeRedayList_;
        struct timeval   wait_ts_ = {};
    };
}  // namespace foundation
}  // namespace xncc
